package 栈和队列;

import java.util.Stack;

/**
 * @description:
 * @author: ywk
 * @date: 2020-07-01
 */
public class StackSorter {
    public static Stack sort(Stack<Integer> s) {
        Stack<Integer> r = new Stack();
        while (!s.isEmpty()) {
            int temp = s.pop();
            while (!r.isEmpty() && r.peek() > temp) {
                s.push(r.pop());
            }
            r.push(temp);
        }
        return r;
    }

    public static void print(Stack stack) {
        while (!stack.isEmpty()) {
            System.out.println(stack.pop());
        }
    }
    public static void main(String[] args) {
        Stack stack = new Stack();
        stack.push(5);
        stack.push(3);
        stack.push(2);
        stack.push(1);
        stack.push(4);

        print( sort(stack));
    }
}
